/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Admin
 */
public class XPathUtility {

    private File fiName;
    String tenchuyenmuc;

    public XPathUtility(File fiName, String tenchuyenmuc) {
        this.fiName = fiName;
        this.tenchuyenmuc = tenchuyenmuc;
    }

    public boolean ktTrung(String s1) {
        if(s1==null)return false;
        try {
            
            FileInputStream file = new FileInputStream(new File(this.fiName.getCanonicalPath()));

            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document xmlDocument = builder.parse(file);

            javax.xml.xpath.XPath xPath = XPathFactory.newInstance().newXPath();
            String expression = "//*/chuyenmuc[@name=\"" + this.tenchuyenmuc + "\"]//baibao";

            NodeList node = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
            for (int i = 0; i < node.getLength(); i++) {
                Element e = (Element) node.item(i);
                NodeList CTBaiBao = e.getElementsByTagName("link");
                String s2 = CTBaiBao.item(0).getTextContent();
                if (s1.equals(s2)) {
                    return true;
                }
            }

        } catch (FileNotFoundException e) {
        } catch (SAXException | ParserConfigurationException | XPathExpressionException e) {
        } catch (IOException e) {
        }
        return false;
    }
}
